package com.group18.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group18.entity.FamilyUser;
import com.group18.service.FamilyUserService;



/**
 * Servlet implementation class DoAddFamily
 */
@WebServlet("/DoAddFamily")
public class DoAddFamily extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoAddFamily() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//防止乱码
		request.setCharacterEncoding("utf-8");
		String family_name = request.getParameter("family_name");
		String msg = "";
		
		//添加家庭
		FamilyUser fu = new FamilyUser();
		fu.setName(family_name);
		FamilyUserService fus = new FamilyUserService();
		boolean bl = fus.isExist(fu);
		if(bl) {
			msg = "该家庭已存在";
			request.setAttribute("msg", msg);
		}else {
			int i = fus.addUser(fu);
			if(i > 0) {
				msg = "添加成功";
				request.setAttribute("msg", msg);
			}else {
				msg = "添加失败";
				request.setAttribute("msg", msg);
			}
		}
		//若是经过搜索操作后的添加家庭操作，添加家庭后返回搜索后的页面
		String jump = request.getParameter("jump");
		if(jump != null && !jump.equals("")) {
			String text = request.getParameter("text");
			if(text != null && !text.equals("")) {
				request.setAttribute("text", text);
				request.getRequestDispatcher("/DoSearchFamliy").forward(request, response);
			}else {
				request.getRequestDispatcher(jump).forward(request, response);
			}
		}else {
			request.getRequestDispatcher("/ToLogin").forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
